<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class File extends a_base {
    public $default_file = './static/upload';
    public $file_type= array('jpg', 'gif', 'png', 'jpeg','bmp');
    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $pagesize = 10;        
        $page = intval($this->input->get('page'));
        if ($page <= 1) {
            $page = 1;
        }
        $this->load->model('m_file');
        $path = $this->input->get('path');
        $path = $this->m_file->get_file($path);
        if (!file_exists($this->default_file . $path)) {
            $path = '';
        }
        
        $dir_list = $this->m_file->dir_list($this->default_file . $path,$this->file_type);
        $list = array_chunk($dir_list, $pagesize);
        $data['dir_list'] = array();
        if (isset($list[$page - 1])) {
            $data['dir_list'] = $list[$page - 1];
        }
        if(empty($data['dir_list']) && $page !=1 ){
            redirect('/file?path='.$path);
            exit;
        }
        $data['default_file'] = $this->default_file;
        $data['pagesize'] = $pagesize;
        $data['page'] = $page;
        $data['path'] = $path;
        $data['count'] = count($dir_list);
        $data['pagecount'] = count($list);

        $this->load->view('file/index', $data);
    }

    function create_folder() {
        $this->load->model('m_file');       
        $foldername = $this->input->post('foldername');      
        //$foldername = iconv('utf-8', 'gbk', $foldername);
        $path = $this->input->post('dir');
        $path=  $this->default_file.$this->m_file->get_file($path);
        if (!file_exists($path)) {
            echo json_encode(array('msg'=>'请选择正确文件路径','code'=>0));
            exit;
        }
        $path = $path.'/'.$foldername;
        if (file_exists($path)) {
            echo json_encode(array('msg'=>'文件已存在','code'=>0));
            exit;
        }
        mkdir($path,0755);
        echo json_encode(array('msg'=>'创建成功','code'=>1));
        exit;
    }
    
    function del_file(){
        $this->load->model('m_file');
        $file = $this->input->post('file');
        $dir = $this->input->post('dir');
        $dir = $this->m_file->get_file($dir);
        $list = explode('/', $file);        
        foreach($list as $k =>$v){
            $filename = $this->default_file.$dir.'/'.$v;
            if(is_dir($filename)){              
                $this->m_file->deldir($filename,TRUE);
            }else{
                $last = substr($filename, strrpos($filename, '.') + 1);
                if(in_array($last, $this->file_type)){
                    unlink($filename);
                }
            }             
        }
        echo json_encode(array('msg'=>'操作成功','code'=>1));
        exit;
    }

    function exists(){
       $filename = $this->input->post('filename');      
       $folderpath = $this->input->post('folderpath');
       //file_exists($filename)
    }
    public function add() {
        $data['doc_id'] =  $this->input->get('doc_id');
        $this->load->view('file/add',$data);
    }

}
